package com.baomidou.springwind.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.support.json.JSONUtils;
import com.baomidou.framework.controller.SuperController;
import com.baomidou.kisso.annotation.Action;
import com.baomidou.kisso.annotation.Login;
import com.baomidou.kisso.annotation.Permission;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.springwind.common.HoorayosHelper;
import com.baomidou.springwind.common.SHA1Helper;
import com.baomidou.springwind.entity.App;
import com.baomidou.springwind.entity.AppCategory;
import com.baomidou.springwind.entity.Member;
import com.baomidou.springwind.entity.Permission1;
import com.baomidou.springwind.entity.Setting;
import com.baomidou.springwind.entity.Wallpaper;
import com.baomidou.springwind.service.IMemberService;
import com.baomidou.springwind.service.IPermission1Service;
import com.baomidou.springwind.service.ISettingService;

/**
 * <p>
 *  网站控制器
 * </p>
 *
 * @author Koumiba
 * @since 2017-03-24
 */
@Controller
@RequestMapping("/sysapp/websitesetting")
public class WebsiteSettingController extends SuperController {
	@Autowired
	private IMemberService memberService;
	@Autowired
	private IPermission1Service permissonService;
	@Autowired
	private ISettingService settingService;
	/**
	 * 主页
	 */
	@Login(action = Action.Skip)
	@Permission(action = Action.Skip)
	@RequestMapping("/index")
	public String index(Model model) {
		model.addAttribute("errorCode", HoorayosHelper.getErrorCodeMap());
		//验证是否登入
		if(!HoorayosHelper.checkLogin(request)){
			model.addAttribute("code", HoorayosHelper.getErrorCode("noLogin"));
			return "error";
		}
		//验证是否管理员
		int mid = HoorayosHelper.getMemberId(request);
		Member user = memberService.selectById(mid);
		if(!HoorayosHelper.checkAdmin(user)){
			model.addAttribute("code", HoorayosHelper.getErrorCode("noAdmin"));
			return "error";
		}
		// 验证权限
		int userPermissionId = user.getPermissionId().intValue(); 
		String appsId = permissonService.selectById(userPermissionId).getAppsId();
		if(!HoorayosHelper.checheckPermissions(appsId, "5")){
			model.addAttribute("code", HoorayosHelper.getErrorCode("noPermissions"));
			return "error";
		}
		Setting setting = new Setting().selectById(1);
		model.addAttribute("set", setting);
		model.addAttribute("global_title", "index");
		
		return "/sysapp/websitesetting/index";
	}
	
	/**
	 * 默认设置
	 */
	@Login(action = Action.Skip)
	@Permission(action = Action.Skip)
	@RequestMapping("/defaultset")
	public String defaultset(Model model) {
		model.addAttribute("errorCode", HoorayosHelper.getErrorCodeMap());
		//验证是否登入
		if(!HoorayosHelper.checkLogin(request)){
			model.addAttribute("code", HoorayosHelper.getErrorCode("noLogin"));
			return "error";
		}
		//验证是否管理员
		int mid = HoorayosHelper.getMemberId(request);
		Member user = memberService.selectById(mid);
		if(!HoorayosHelper.checkAdmin(user)){
			model.addAttribute("code", HoorayosHelper.getErrorCode("noAdmin"));
			return "error";
		}
		// 验证权限
		int userPermissionId = user.getPermissionId().intValue(); 
		String appsId = permissonService.selectById(userPermissionId).getAppsId();
		if(!HoorayosHelper.checheckPermissions(appsId, "5")){
			model.addAttribute("code", HoorayosHelper.getErrorCode("noPermissions"));
			return "error";
		}
		
		Setting set = new Setting().selectById(1);
		model.addAttribute("set", set);
		if(!set.getDock().equals("")){
			EntityWrapper ew = new EntityWrapper<App>();
			ew.in("tbid", set.getDock());
			ew.setSqlSelect("tbid, name, icon");
			List<App> appList = new App().selectList(ew);
			model.addAttribute("dockinfo", appList);	
		}
		if(!set.getDesk1().equals("")){
			EntityWrapper ew = new EntityWrapper<App>();
			ew.in("tbid", set.getDesk1());
			ew.setSqlSelect("tbid, name, icon");
			List<App> appList = new App().selectList(ew);
			model.addAttribute("desk1info", appList);	
		}
		if(!set.getDesk2().equals("")){
			EntityWrapper ew = new EntityWrapper<App>();
			ew.in("tbid", set.getDesk2());
			ew.setSqlSelect("tbid, name, icon");
			List<App> appList = new App().selectList(ew);
			model.addAttribute("desk2info", appList);	
		}
		if(!set.getDesk3().equals("")){
			EntityWrapper ew = new EntityWrapper<App>();
			ew.in("tbid", set.getDesk3());
			ew.setSqlSelect("tbid, name, icon");
			List<App> appList = new App().selectList(ew);
			model.addAttribute("desk3info", appList);	
		}
		if(!set.getDesk4().equals("")){
			EntityWrapper ew = new EntityWrapper<App>();
			ew.in("tbid", set.getDesk4());
			ew.setSqlSelect("tbid, name, icon");
			List<App> appList = new App().selectList(ew);
			model.addAttribute("desk4info", appList);	
		}
		if(!set.getDesk5().equals("")){
			EntityWrapper ew = new EntityWrapper<App>();
			ew.in("tbid", set.getDesk5());
			ew.setSqlSelect("tbid, name, icon");
			List<App> appList = new App().selectList(ew);
			model.addAttribute("desk5info", appList);	
		}
		List<Wallpaper> wallpaperList =	new Wallpaper().selectAll();
		model.addAttribute("wallpaperList", wallpaperList); //壁纸
		model.addAttribute("global_title", "defaultset");
		return "/sysapp/websitesetting/defaultset";
	}
	/**
	 *  添加应用
	 *	 */
	@Login(action = Action.Skip)
	@Permission(action = Action.Skip)
	@RequestMapping("/alert_addapps")
	public String addapps(Model model) {
		EntityWrapper ew = new EntityWrapper<AppCategory>();
		ew.where("issystem=0");
		List<AppCategory> acList = new AppCategory().selectList(ew);
		model.addAttribute("category", acList);
		
		String content = "";
		for(AppCategory c : acList){
			content += "<div class='alert_addapps tab-pane' id='category_"+c.getTbid()+"'>";
			ew = new EntityWrapper<App>();
			ew.where("app_category_id="+c.getTbid()+"	AND verifytype=1");
			List<App> appList = new App().selectList(ew);
			for(App v : appList){
				content += "<div class='app' title='"+v.getName()+"' appid='"+v.getTbid()+"'>";
				content += "<img src= '"+v.getIcon()+"' alt='"+v.getName()+"' title='"+v.getName()+"'>";
				content += "<div class='name'>"+v.getName()+"</div>";
				content += "<span class='selected'></span>";
				content += "</div>";
			}	
			content += "</div>";
		}
		model.addAttribute("content", content);
		return "/sysapp/websitesetting/alert_addapps";
	}
//	alert_addapps
	/**
	 * ajax页面
	 */
	@ResponseBody
	@Login(action = Action.Skip)
	@Permission(action = Action.Skip)
	@RequestMapping("/ajax")
	public String ajax(String ac) {
		String display = "";
		if (ac.equals("edit")){
			String title = request.getParameter("val_title");
			String keywords = request.getParameter("val_keywords");
			String description = request.getParameter("val_description");
			
			Setting set = new Setting().selectById(1);
			set.setTitle(title);
			set.setKeywords(keywords);
			set.setDescription(description);
			settingService.insertOrUpdate(set);
			
			Map<String,String> result = new HashMap<String,String>();
			result.put("info", "");
			result.put("status", "y");
			return JSONUtils.toJSONString(result);
		}
		return display;
	}
	/**
	 * ajax页面
	 */
	@ResponseBody
	@Login(action = Action.Skip)
	@Permission(action = Action.Skip)
	@RequestMapping("/defaultsetAjax")
	public String defaultsetAjax(String ac) {
		if (ac.equals("edit")){
			Setting set = new Setting().selectById(1);
			set.setIsforcedlogin(Integer.parseInt(request.getParameter("val_isforcedlogin")));
			set.setDock(request.getParameter("val_dock"));
			set.setDesk1(request.getParameter("val_desk1"));
			set.setDesk2(request.getParameter("val_desk2"));
			set.setDesk3(request.getParameter("val_desk3"));
			set.setDesk4(request.getParameter("val_desk4"));
			set.setDesk5(request.getParameter("val_desk5"));
			if(request.getParameter("desk")!=null)
				set.setDesk(Integer.parseInt(request.getParameter("desk")));
			set.setAppxy(request.getParameter("val_appxy"));
			set.setAppsize(Integer.parseInt(request.getParameter("val_appsize")));
			set.setAppverticalspacing(Integer.parseInt(request.getParameter("val_appverticalspacing")));
			set.setApphorizontalspacing(Integer.parseInt(request.getParameter("val_apphorizontalspacing")));
			set.setDockpos(request.getParameter("val_dockpos"));
			set.setSkin(request.getParameter("val_skin"));
			set.setWallpaperId(Integer.parseInt(request.getParameter("val_wallpaper_id")));
			set.setWallpapertype(request.getParameter("val_wallpapertype"));
				
			settingService.insertOrUpdate(set);
			Map<String,String> result = new HashMap<String,String>();
			result.put("info", "");
			result.put("status", "y");
			return JSONUtils.toJSONString(result);
		}else if(ac.equals("updateApps")){
			String appsid = request.getParameter("appsid");
			EntityWrapper ew = new EntityWrapper<App>();
			ew.in("tbid", appsid);
			ew.setSqlSelect("tbid, name, icon");
			List<App> appList = new App().selectList(ew);
			String display = "";
			for(App v : appList){
				display += "<div class='app' appid='"+v.getTbid()+"'><img src='../../"+v.getIcon()+"' alt='"+v.getName()+"' title='"+v.getName()+"'><span class='del'>删</span></div>";
			}
			return display;
		}
		return "";
	}
}
